package org.itboys.seller;

import com.google.common.collect.Lists;
import org.itboys.admin.entity.BusinessCenterAdminMenu;
import org.itboys.admin.entity.BusinessCenterUser;
import org.itboys.admin.service.BusinessCenterAdminMenuService;
import org.itboys.admin.service.BusinessCenterUserService;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.seller.service.CaliforniaSiteUserService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InitBusinessCenterAdminDataService implements InitializingBean {
	
	@Autowired
	private CaliforniaSiteUserService californiaSiteUserService;
	@Autowired
	private BusinessCenterAdminMenuService businessCenterAdminMenuService;
	
	public static String username;//用户名
	
	private String adminMenuUrl; //系统管理url
	private String menuManageUrl;//菜单管理url
	private String userManageUrl;//用户管理url
    private String roleManagerUrl;
    private String sellerManagerUrl;
    private String touristManagerUrl;
	
	private String systemConfigUrl;// 配置管理url
	private String dictionaryManagerUrl;// 
	private String paramTemplateManagerUrl;// 
	private String emailTemplateManagerUrl;// 
	private String msgTemplateManagerUrl;//

    private String productConfigUrl;
    private String productListUrl;
    private String productSourceUrl;
    private String productCheckUrl;
	
	/* 超级用户名  */
	private static final String SYSTEM_ROLE_SUPER = username;
	/* 顶级菜单名  */
	private static final String SYSTEM_MENU_SUPER = "系统管理";
	/* 配置管理 */
	private static final String SYSTEM_CONFIG_SUPER = "配置管理";
    /*产品管理*/
    private static final String SYSTEM_PRODUCT_SUPER = "业绩管理";
	
	private static final String FA_DESKTOP = "fa-desktop";// 系统管理样式
	private static final String FA_COG = "fa-cog";// 配置管理样式
	
	@Override
	public void afterPropertiesSet() throws Exception {
		
		/* 若数据库中没有admin超级用户，自动添加超级管理员   */
		if(!californiaSiteUserService.exists("memberName", username)){
			BusinessCenterUser au = new BusinessCenterUser();
			au.setUsername(username);
			au.setName(username);
			au.setPassword(Digests.md5("123456"));
			au.setType(1);
			//californiaSiteUserService.saveWithoutLogin(au);
		}
		
		/* 若没有顶级菜单，则自动创建   */
		if(!businessCenterAdminMenuService.exists("menuName", SYSTEM_MENU_SUPER)){
			List<BusinessCenterAdminMenu> menuList = Lists.newArrayListWithExpectedSize(8);
			/* 创建顶级菜单  */
			BusinessCenterAdminMenu am = new BusinessCenterAdminMenu();
			am.setMenuName(SYSTEM_MENU_SUPER);
			am.setMenuClass(FA_DESKTOP);
			am.setUrl(adminMenuUrl);
			am.setFullPath(adminMenuUrl);
			am.setParentId(0l);
			am.setLevel(1);
			am.setSort(1);
            businessCenterAdminMenuService.saveWithoutLogin(am);
			
			/* 子菜单:菜单管理   */
			BusinessCenterAdminMenu am2 = new BusinessCenterAdminMenu();
			am2.setMenuName("菜单管理");
			am2.setUrl(menuManageUrl);
			am2.setFullPath(menuManageUrl);
			am2.setParentId(am.getId());
			am2.setSort(1);
			am2.setLevel(2);
            am2.setCenterId(0);

			menuList.add(am2);

            businessCenterAdminMenuService.saveWithoutLogin(menuList);
		}
		
//		/* 若没有配置管理菜单，则自动创建   */
//		if(!businessCenterAdminMenuService.exists("menuName", SYSTEM_CONFIG_SUPER)){
//			List<BusinessCenterAdminMenu> menuList = Lists.newArrayListWithExpectedSize(5);
//			/* 创建配置管理菜单  */
//            BusinessCenterAdminMenu am = new BusinessCenterAdminMenu();
//			am.setMenuName(SYSTEM_CONFIG_SUPER);
//			am.setMenuClass(FA_COG);
//			am.setUrl(systemConfigUrl);
//			am.setFullPath(systemConfigUrl);
//			am.setParentId(0l);
//			am.setLevel(1);
//			am.setSort(2);
//            businessCenterAdminMenuService.saveWithoutLogin(am);
//
//			/* 子菜单:数据字典   */
//			BusinessCenterAdminMenu am2 = new BusinessCenterAdminMenu();
//			am2.setMenuName("数据字典");
//			am2.setUrl(dictionaryManagerUrl);
//			am2.setFullPath(dictionaryManagerUrl);
//			am2.setParentId(am.getId());
//			am2.setSort(1);
//			am2.setLevel(2);
//
//			/* 子菜单:参数模板   */
//			BusinessCenterAdminMenu am3 = new BusinessCenterAdminMenu();
//			am3.setMenuName("参数模板");
//			am3.setUrl(paramTemplateManagerUrl);
//			am3.setFullPath(paramTemplateManagerUrl);
//			am3.setParentId(am.getId());
//			am3.setSort(2);
//			am3.setLevel(2);
//
//			/* 子菜单:邮件模板   */
//			BusinessCenterAdminMenu am4 = new BusinessCenterAdminMenu();
//			am4.setMenuName("邮件模板");
//			am4.setUrl(emailTemplateManagerUrl);
//			am4.setFullPath(emailTemplateManagerUrl);
//			am4.setParentId(am.getId());
//			am4.setSort(3);
//			am4.setLevel(2);
//
//			/* 子菜单:短信模板   */
//			BusinessCenterAdminMenu am5 = new BusinessCenterAdminMenu();
//			am5.setMenuName("短信模板");
//			am5.setUrl(msgTemplateManagerUrl);
//			am5.setFullPath(msgTemplateManagerUrl);
//			am5.setParentId(am.getId());
//			am5.setSort(4);
//			am5.setLevel(2);
//
//			menuList.add(am2);
//			menuList.add(am3);
//			menuList.add(am4);
//			menuList.add(am5);
//
//            businessCenterAdminMenuService.saveWithoutLogin(menuList);
//		}

        /* 若没有配置管理菜单，则自动创建   */
        if(!businessCenterAdminMenuService.exists("menuName", SYSTEM_PRODUCT_SUPER)){
            List<BusinessCenterAdminMenu> menuList = Lists.newArrayListWithExpectedSize(5);
			/* 创建配置管理菜单  */
            BusinessCenterAdminMenu am = new BusinessCenterAdminMenu();
            am.setMenuName(SYSTEM_PRODUCT_SUPER);
            am.setMenuClass(FA_COG);
            am.setUrl(productConfigUrl);
            am.setFullPath(productConfigUrl);
            am.setParentId(0l);
            am.setLevel(1);
            am.setSort(2);
            businessCenterAdminMenuService.saveWithoutLogin(am);

			/* 子菜单:产品库   */
            BusinessCenterAdminMenu am2 = new BusinessCenterAdminMenu();
            am2.setMenuName("产品库");
            am2.setUrl(productListUrl);
            am2.setFullPath(productListUrl);
            am2.setParentId(am.getId());
            am2.setSort(1);
            am2.setLevel(2);

			/* 子菜单:货源地管理   */
            BusinessCenterAdminMenu am3 = new BusinessCenterAdminMenu();
            am3.setMenuName("货源地管理");
            am3.setUrl(productSourceUrl);
            am3.setFullPath(productSourceUrl);
            am3.setParentId(am.getId());
            am3.setSort(2);
            am3.setLevel(2);

			/* 子菜单:产品审核   */
            BusinessCenterAdminMenu am4 = new BusinessCenterAdminMenu();
            am4.setMenuName("产品审核");
            am4.setUrl(productCheckUrl);
            am4.setFullPath(productCheckUrl);
            am4.setParentId(am.getId());
            am4.setSort(3);
            am4.setLevel(2);

            menuList.add(am2);
            menuList.add(am3);
            menuList.add(am4);

            businessCenterAdminMenuService.saveWithoutLogin(menuList);
        }
		
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setAdminMenuUrl(String adminMenuUrl) {
		this.adminMenuUrl = adminMenuUrl;
	}

	public void setMenuManageUrl(String menuManageUrl) {
		this.menuManageUrl = menuManageUrl;
	}

	public void setUserManageUrl(String userManageUrl) {
		this.userManageUrl = userManageUrl;
	}

	public void setSystemConfigUrl(String systemConfigUrl) {
		this.systemConfigUrl = systemConfigUrl;
	}

	public void setDictionaryManagerUrl(String dictionaryManagerUrl) {
		this.dictionaryManagerUrl = dictionaryManagerUrl;
	}

	public void setParamTemplateManagerUrl(String paramTemplateManagerUrl) {
		this.paramTemplateManagerUrl = paramTemplateManagerUrl;
	}

	public void setEmailTemplateManagerUrl(String emailTemplateManagerUrl) {
		this.emailTemplateManagerUrl = emailTemplateManagerUrl;
	}

	public void setMsgTemplateManagerUrl(String msgTemplateManagerUrl) {
		this.msgTemplateManagerUrl = msgTemplateManagerUrl;
	}

    public void setRoleManagerUrl(String roleManagerUrl) {
        this.roleManagerUrl = roleManagerUrl;
    }

    public void setSellerManagerUrl(String sellerManagerUrl) {
        this.sellerManagerUrl = sellerManagerUrl;
    }

    public void setTouristManagerUrl(String touristManagerUrl) {
        this.touristManagerUrl = touristManagerUrl;
    }

    public void setProductConfigUrl(String productConfigUrl) {
        this.productConfigUrl = productConfigUrl;
    }

    public void setProductListUrl(String productListUrl) {
        this.productListUrl = productListUrl;
    }

    public void setProductSourceUrl(String productSourceUrl) {
        this.productSourceUrl = productSourceUrl;
    }

    public void setProductCheckUrl(String productCheckUrl) {
        this.productCheckUrl = productCheckUrl;
    }
}
